{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import xml.etree.ElementTree as ET"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "def parse_label(xml_file):\n",
    "    #建立一个实例\n",
    "    tree = ET.parse(xml_file)\n",
    "    \n",
    "    #建立跟路径\n",
    "    root = tree.getroot()\n",
    "    \n",
    "    width = root.find('size').find('width').text\n",
    "    height = root.find('size').find('height').text\n",
    "    image_name = root.find('filename').text\n",
    "    \n",
    "    #数据列表\n",
    "    category = []\n",
    "    xmin = []\n",
    "    ymin = []\n",
    "    xmax = []\n",
    "    ymax = []\n",
    "    \n",
    "    for object in root.findall('object'):\n",
    "        for x in object.iter('name'):\n",
    "            category.append(x.text)\n",
    "            xmax.append(object.find('bndbox').find('xmax').text)\n",
    "            ymax.append(object.find('bndbox').find('ymax').text)\n",
    "            xmin.append(object.find('bndbox').find('xmin').text)\n",
    "            ymin.append(object.find('bndbox').find('ymin').text)\n",
    "    \n",
    "    #列表组合\n",
    "    ground_truth_box = [list(box) for box in zip(xmin,ymin,xmax,ymax)]\n",
    "    \n",
    "    return image_name,(width,height),category,ground_truth_box\n",
    "     "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('ILSVRC2012_val_00000021.JPEG', ('375', '500'), ['noidea', 'noidea'], [['213', '241', '333', '241'], ['50', '353', '132', '353']]) \n",
      "\n",
      "('ILSVRC2012_val_00000022.JPEG', ('375', '500'), ['person', 'dog'], [['12', '487', '126', '487'], ['201', '500', '295', '500']]) \n",
      "\n",
      "('ILSVRC2012_val_00000023.JPEG', ('709', '510'), ['apple', 'apple'], [['66', '353', '241', '353'], ['379', '429', '590', '429']]) \n",
      "\n",
      "('ILSVRC2012_val_00000026.JPEG', ('375', '500'), ['lamp', 'table'], [['29', '294', '363', '294'], ['12', '490', '368', '490']]) \n",
      "\n",
      "('ILSVRC2012_val_00000031.JPEG', ('500', '426'), ['fffff', 'butterfly'], [['91', '312', '269', '312'], ['93', '305', '269', '305']]) \n",
      "\n"
     ]
    }
   ],
   "source": [
    "import glob  \n",
    "for name in glob.glob('/Users/mikegao/Desktop/Annotation/*'):\n",
    "    print (parse_label(name),'\\n')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[{'name': 11, 'age': 10}, {'name': 12, 'age': 11}, {'name': 13, 'age': 12}, {'name': 14, 'age': 13}, {'name': 15, 'age': 14}, {'name': 16, 'age': 15}, {'name': 17, 'age': 16}, {'name': 18, 'age': 17}, {'name': 19, 'age': 18}, {'name': 20, 'age': 19}]\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "list_table =[]\n",
    "for i in range(10,20):\n",
    "    date = {'name':i+1,\"age\":i}\n",
    "    list_table.append(date)\n",
    "print (list_table)\n",
    "\n",
    "data_frame = pd.DataFrame(data=list_table,columns=['age','name'])\n",
    "data_frame.to_csv('321.csv',index=False,mode='a',header=True)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
